<!DOCTYPE html>
<!-- saved from url=(0041)http://c.biancheng.net/view/vip_7059.html -->
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript" async="" charset="utf-8" src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/core.php"></script><script async="" src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/click.php"></script>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<!-- 启用Chromium高速渲染模式 -->
<meta name="renderer" content="webkit">
<meta name="force-rendering" content="webkit">
<!-- 禁止百度转码 -->
<meta name="applicable-device" content="pc,mobile">
<meta name="MobileOptimized" content="width">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<!-- 禁止识别电话号码 -->
<meta name="format-detection" content="telephone=no">

<link rel="shortcut icon" href="http://c.biancheng.net/favicon.ico?v=1.6.72">
<link href="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/common.css" rel="stylesheet">
<title>Java项目实战：数字转人民币读法（附带源码和解析）</title>
<meta name="description" content="本节讲解在实际开发中常用的一个方法：将一个浮点数转换成人民币读法字符串，这个程序就需要使用数组。 实现这个方法的思路是，首先把这个浮点数分成整数部分和小数部分。提取">
</head>
<body>
<div id="topbar" class="clearfix">
	<ul id="product-type" class="left">
		<li>
			<a href="http://c.biancheng.net/"><span class="iconfont iconfont-home"></span>首页</a>
		</li>
		<li class="active">
			<a href="http://c.biancheng.net/sitemap/" rel="nofollow"><span class="iconfont iconfont-book"></span>教程</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/vip/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-vip"></span>VIP会员</a>
		</li>
		<li>
			<a href="http://vip.biancheng.net/p/q2a/show.php" rel="nofollow" target="_blank"><span class="iconfont iconfont-q2a"></span>一对一答疑</a>
		</li>
		<li>
			<a href="http://fudao.biancheng.net/" rel="nofollow" target="_blank"><span class="iconfont iconfont-fudao"></span>辅导班</a>
		</li>
	</ul>
</div>
<div id="header" class="clearfix">
	<a id="logo" class="left" href="http://c.biancheng.net/">
		<img height="26" src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/logo.png" alt="C语言中文网">
	</a>
	<ul id="nav-main" class="hover-none left clearfix">
		<li class="wap-yes"><a href="http://c.biancheng.net/">首页</a></li>
		<li><a href="http://c.biancheng.net/c/">C语言教程</a></li>
		<li><a href="http://c.biancheng.net/cplus/">C++教程</a></li>
		<li><a href="http://c.biancheng.net/python/">Python教程</a></li>
		<li><a href="http://c.biancheng.net/java/">Java教程</a></li>
		<li><a href="http://c.biancheng.net/linux_tutorial/">Linux入门</a></li>
		<li><a href="http://c.biancheng.net/sitemap/" title="网站地图">更多&gt;&gt;</a></li>
	</ul>
	<span id="sidebar-toggle" class="toggle-btn" toggle-target="#sidebar">目录 <span class="glyphicon"></span></span>

	<a href="http://vip.biancheng.net/?from=topbar" class="user-info glyphicon glyphicon-user hover-none" target="_blank" rel="nofollow" title="用户中心"></a>
</div>
<div id="main" class="clearfix">
	<div id="sidebar" class="toggle-target">
	<div id="contents">
		<dt><span class="glyphicon glyphicon-option-vertical" aria-hidden="true"></span><a href="http://c.biancheng.net/java/">Java教程</a></dt>
		
			<dd>
				<span class="channel-num">1</span>
				<a href="http://c.biancheng.net/java/10/">Java入门基础及环境搭建</a>
			</dd>
		
			<dd>
				<span class="channel-num">2</span>
				<a href="http://c.biancheng.net/java/20/">Java程序设计基础</a>
			</dd>
		
			<dd>
				<span class="channel-num">3</span>
				<a href="http://c.biancheng.net/java/30/">Java流程控制语句</a>
			</dd>
		
			<dd>
				<span class="channel-num">4</span>
				<a href="http://c.biancheng.net/java/40/">Java字符串处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">5</span>
				<a href="http://c.biancheng.net/java/50/">Java数字和日期处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">6</span>
				<a href="http://c.biancheng.net/java/60/">Java内置包装类</a>
			</dd>
		<dd class="this"> <span class="channel-num">7</span> <a href="http://c.biancheng.net/java/70/">Java数组处理</a> </dd><dl class="dl-sub"><dd>7.1 <a href="http://c.biancheng.net/view/906.html">Java数组简介</a></dd><dd>7.2 <a href="http://c.biancheng.net/view/5852.html">Java一维数组</a></dd><dd>7.3 <a href="http://c.biancheng.net/view/5857.html">Java二维数组</a></dd><dd>7.4 <a href="http://c.biancheng.net/view/917.html">Java多维数组</a></dd><dd>7.5 <a href="http://c.biancheng.net/view/5863.html">Java不规则数组</a></dd><dd>7.6 <a href="http://c.biancheng.net/view/5843.html">Java数组也是一种数据类型</a></dd><dd>7.7 <a href="http://c.biancheng.net/view/vip_7057.html">Java中到底有没有多维数组（长篇神文）？</a><span class="glyphicon glyphicon-usd"></span></dd><dd>7.8 <a href="http://c.biancheng.net/view/5885.html">Java Arrays工具类</a></dd><dd>7.9 <a href="http://c.biancheng.net/view/vip_7058.html">Java中数组和字符串的相互转换</a><span class="glyphicon glyphicon-usd"></span></dd><dd>7.10 <a href="http://c.biancheng.net/view/919.html">Java比较数组</a></dd><dd>7.11 <a href="http://c.biancheng.net/view/921.html">Java数组填充</a></dd><dd>7.12 <a href="http://c.biancheng.net/view/923.html">Java数组查找指定元素</a></dd><dd>7.13 <a href="http://c.biancheng.net/view/924.html">Java复制数组</a></dd><dd>7.14 <a href="http://c.biancheng.net/view/925.html">Java sort()数组排序</a></dd><dd>7.15 <a href="http://c.biancheng.net/view/927.html">Java冒泡排序法</a></dd><dd>7.16 <a href="http://c.biancheng.net/view/929.html">Java快速排序法</a></dd><dd>7.17 <a href="http://c.biancheng.net/view/930.html">Java选择排序法</a></dd><dd>7.18 <a href="http://c.biancheng.net/view/931.html">Java直接插入排序法</a></dd><dd>7.19 <a href="http://c.biancheng.net/view/5891.html">对Java数组的总结</a></dd><dd>7.20 <a href="http://c.biancheng.net/view/5855.html">Java求数组元素的最大和最小值</a></dd><dd class="active">7.21 <a href="http://c.biancheng.net/view/vip_7059.html">【Java项目实战】数字转人民币读法</a><span class="glyphicon glyphicon-usd"></span></dd><dd>7.22 <a href="http://c.biancheng.net/view/915.html">Java统计商品信息</a></dd><dd>7.23 <a href="http://c.biancheng.net/view/933.html">Java商品信息查询</a></dd><dd>7.24 <a href="http://c.biancheng.net/view/vip_7060.html">【Java项目实战】输出杨辉三角形（使用二维数组）</a><span class="glyphicon glyphicon-usd"></span></dd><dd>7.25 <a href="http://c.biancheng.net/view/vip_7061.html">【Java项目实战】五子棋游戏</a><span class="glyphicon glyphicon-usd"></span></dd></dl>
			<dd>
				<span class="channel-num">8</span>
				<a href="http://c.biancheng.net/java/80/">Java类和对象</a>
			</dd>
		
			<dd>
				<span class="channel-num">9</span>
				<a href="http://c.biancheng.net/java/90/">Java继承和多态</a>
			</dd>
		
			<dd>
				<span class="channel-num">10</span>
				<a href="http://c.biancheng.net/java/100/">Java异常处理</a>
			</dd>
		
			<dd>
				<span class="channel-num">11</span>
				<a href="http://c.biancheng.net/java/110/">Java集合、泛型和枚举</a>
			</dd>
		
			<dd>
				<span class="channel-num">12</span>
				<a href="http://c.biancheng.net/java/120/">Java反射机制</a>
			</dd>
		
			<dd>
				<span class="channel-num">13</span>
				<a href="http://c.biancheng.net/java/130/">Java输入/输出流</a>
			</dd>
		
			<dd>
				<span class="channel-num">14</span>
				<a href="http://c.biancheng.net/java/java/135/">Java注解</a>
			</dd>
		
	</div>
</div>
	<div id="article-wrap">
		<div id="article">
			<div class="arc-info">
	<span class="position"><span class="glyphicon glyphicon-map-marker"></span> <a href="http://c.biancheng.net/">首页</a> &gt; <a href="http://c.biancheng.net/java/">Java教程</a> &gt; <a href="http://c.biancheng.net/java/70/">Java数组处理</a></span>
	<span class="read-num">阅读：3,370</span>
</div>

<div id="ad-position-bottom"></div>
			<h1>Java项目实战：数字转人民币读法（附带源码和解析）</h1>
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/5855.html">Java求数组元素的最大和最小值</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/915.html">Java统计商品信息</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-top"><p class="pic"></p><p class="text" adid="java-1v1q2a"><a href="http://c.biancheng.net/view/7552.html" target="_blank" style="color: #D33428;">C语言中文网推出辅导班啦，包括<span style="color: #008000;">「C语言辅导班、C++辅导班、算法/数据结构辅导班」</span>，全部都是一对一教学：一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线，随时响应！</a></p></div>
			<div id="arc-body">本节讲解在实际开发中常用的一个方法：将一个浮点数转换成人民币读法字符串，这个程序就需要使用数组。<br>
<br>
实现这个方法的思路是，首先把这个浮点数分成整数部分和小数部分。提取整数部分很容易，直接将这个浮点数强制类型转换成一个整数即可，这个整数就是浮点数的整数部分。再使用浮点数减去整数将可以得到这个浮点数的小数部分。<br>
<br>
然后分开处理整数部分和小数部分，其中小数部分的处理比较简单，直接截断到保留 2 位数字，转换成几角几分的字符串。整数部分的处理则稍微复杂一点，但只要认真分析不难发现，中国的数字习惯是 4 位一节的，一个 4 位的数字可被转成几千几百几十几，至于后面添加什么单位则不确定：
<ul>
	<li>
		如果这节 4 位数字出现在 1~4 位，则后面添加单位元。</li>
	<li>
		如果这节 4 位数字出现在 5~8 位，则后面添加单位万。</li>
	<li>
		如果这节 4 位数字出现在 9~12 位，则后面添加单位亿。</li>
</ul>
<p class="info-box">
	多于 12 位就暂不考虑了。</p>
因此实现这个程序的关键就是把一个 4 位整数的浮点数转换成一个中文读法。下面程序把这个需求实现了一部分。
<div class="snippet-container" style="undefined;"><div class="sh_bright snippet-wrap"><div class="snippet-menu sh_sourceCode" style="display: none;"><pre><a class="snippet-copy sh_url" href="http://c.biancheng.net/view/vip_7059.html#" style="display: none;">复制</a><a class="snippet-text sh_url" href="http://c.biancheng.net/view/vip_7059.html#">纯文本</a><a class="snippet-window sh_url" href="http://c.biancheng.net/view/vip_7059.html#">复制</a></pre></div><pre class="java sh_java snippet-formatted sh_sourceCode"><ol class="snippet-num"><li><span class="sh_preproc">import</span> java<span class="sh_symbol">.</span>util<span class="sh_symbol">.</span>Arrays<span class="sh_symbol">;</span></li><li><span class="sh_preproc">import</span> java<span class="sh_symbol">.</span>util<span class="sh_symbol">.</span>Scanner<span class="sh_symbol">;</span></li><li><span style="display:none;">&nbsp;</span></li><li><span class="sh_keyword">public</span> <span class="sh_keyword">class</span><span class="sh_normal"> </span><span class="sh_classname">NumRmb</span> <span class="sh_cbracket">{</span></li><li>    <span class="sh_keyword">private</span> String<span class="sh_symbol">[]</span> hanArr <span class="sh_symbol">=</span> <span class="sh_cbracket">{</span> <span class="sh_string">"零"</span><span class="sh_symbol">,</span> <span class="sh_string">"壹"</span><span class="sh_symbol">,</span> <span class="sh_string">"贰"</span><span class="sh_symbol">,</span> <span class="sh_string">"叁"</span><span class="sh_symbol">,</span> <span class="sh_string">"肆"</span><span class="sh_symbol">,</span> <span class="sh_string">"伍"</span><span class="sh_symbol">,</span> <span class="sh_string">"陆"</span><span class="sh_symbol">,</span> <span class="sh_string">"柒"</span><span class="sh_symbol">,</span> <span class="sh_string">"捌"</span><span class="sh_symbol">,</span> <span class="sh_string">"玖"</span> <span class="sh_cbracket">}</span><span class="sh_symbol">;</span></li><li>    <span class="sh_keyword">private</span> String<span class="sh_symbol">[]</span> unitArr <span class="sh_symbol">=</span> <span class="sh_cbracket">{</span> <span class="sh_string">"十"</span><span class="sh_symbol">,</span> <span class="sh_string">"百"</span><span class="sh_symbol">,</span> <span class="sh_string">"千"</span> <span class="sh_cbracket">}</span><span class="sh_symbol">;</span></li><li>    <span class="sh_keyword">private</span> String<span class="sh_symbol">[]</span> unitArr2 <span class="sh_symbol">=</span> <span class="sh_cbracket">{</span> <span class="sh_string">"分"</span><span class="sh_symbol">,</span> <span class="sh_string">"角"</span> <span class="sh_cbracket">}</span><span class="sh_symbol">;</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_comment">/**</span></li><li><span class="sh_comment">     * 把一个浮点数分解成整数部分和小数部分字符串</span></li><li><span class="sh_comment">     *</span></li><li><span class="sh_comment">     * </span><span class="sh_type">@param</span><span class="sh_comment"> num 需要被分解的浮点数</span></li><li><span class="sh_comment">     * </span><span class="sh_type">@return</span><span class="sh_comment"> 分解出来的整数部分和小数部分。第一个数组元素是整数部分，笫二个数组元素是小数部分</span></li><li><span class="sh_comment">     */</span></li><li>    <span class="sh_keyword">private</span> String<span class="sh_symbol">[]</span> <span class="sh_function">divide</span><span class="sh_symbol">(</span><span class="sh_type">double</span> num<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li><span style="display:none;">&nbsp;</span></li><li>        <span class="sh_comment">// 将一个浮点数强制类型转换为long型，即得到它的整数部分</span></li><li>        <span class="sh_type">long</span> zheng <span class="sh_symbol">=</span> <span class="sh_symbol">(</span><span class="sh_type">long</span><span class="sh_symbol">)</span> num<span class="sh_symbol">;</span></li><li>        <span class="sh_comment">// 浮点数减去整数部分，得到小数部分，小数部分乘以100后再取整得到2位小数</span></li><li>        <span class="sh_type">long</span> xiao <span class="sh_symbol">=</span> Math<span class="sh_symbol">.</span><span class="sh_function">round</span><span class="sh_symbol">((</span>num <span class="sh_symbol">-</span> zheng<span class="sh_symbol">)</span> <span class="sh_symbol">*</span> <span class="sh_number">100</span><span class="sh_symbol">);</span></li><li>        <span class="sh_comment">// 下面用了2种方法把整数转换为字符串</span></li><li>        <span class="sh_keyword">return</span> <span class="sh_keyword">new</span> String<span class="sh_symbol">[]</span> <span class="sh_cbracket">{</span> zheng <span class="sh_symbol">+</span> <span class="sh_string">""</span><span class="sh_symbol">,</span> String<span class="sh_symbol">.</span><span class="sh_function">valueOf</span><span class="sh_symbol">(</span>xiao<span class="sh_symbol">)</span> <span class="sh_cbracket">}</span><span class="sh_symbol">;</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_comment">/**</span></li><li><span class="sh_comment">     * 将数字转成人民币读法</span></li><li><span class="sh_comment">     *</span></li><li><span class="sh_comment">     * </span><span class="sh_type">@param</span><span class="sh_comment"> str[] 被分解成整数部分和小数部分的字符串数组</span></li><li><span class="sh_comment">     * </span><span class="sh_type">@return</span><span class="sh_comment"> 人民币读法的汉字字符串</span></li><li><span class="sh_comment">     */</span></li><li>    <span class="sh_keyword">private</span> <span class="sh_usertype">String</span><span class="sh_normal"> </span><span class="sh_function">toHanStr</span><span class="sh_symbol">(</span><span class="sh_usertype">String</span><span class="sh_normal"> </span>str<span class="sh_symbol">[])</span> <span class="sh_cbracket">{</span></li><li><span style="display:none;">&nbsp;</span></li><li>        <span class="sh_type">int</span> zhengLen <span class="sh_symbol">=</span> str<span class="sh_symbol">[</span><span class="sh_number">0</span><span class="sh_symbol">].</span><span class="sh_function">trim</span><span class="sh_symbol">().</span><span class="sh_function">split</span><span class="sh_symbol">(</span><span class="sh_string">""</span><span class="sh_symbol">).</span>length<span class="sh_symbol">;</span> <span class="sh_comment">// 整数的长度</span></li><li>        <span class="sh_type">int</span> xiaoLen <span class="sh_symbol">=</span> str<span class="sh_symbol">[</span><span class="sh_number">1</span><span class="sh_symbol">].</span><span class="sh_function">split</span><span class="sh_symbol">(</span><span class="sh_string">""</span><span class="sh_symbol">).</span>length<span class="sh_symbol">;</span> <span class="sh_comment">// 小数的长度</span></li><li>        <span class="sh_usertype">String</span><span class="sh_normal"> </span>result <span class="sh_symbol">=</span> <span class="sh_string">""</span><span class="sh_symbol">;</span></li><li>        <span class="sh_comment">// 依次遍历整数的每一位数字</span></li><li>        <span class="sh_keyword">for</span> <span class="sh_symbol">(</span><span class="sh_type">int</span> i <span class="sh_symbol">=</span> <span class="sh_number">0</span><span class="sh_symbol">;</span> i <span class="sh_symbol">&lt;</span> zhengLen<span class="sh_symbol">;</span> i<span class="sh_symbol">++)</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_comment">// 把char型数字转换成int型数字，3 种方法如下：</span></li><li>            <span class="sh_comment">// 方法1：因为它们的ASCII码值恰好相差48，因此把char型数字减去48得到int型数字，例如‘4’被转换成4</span></li><li>            <span class="sh_comment">// int num = str[0].charAt(i) - 48;</span></li><li>            <span class="sh_comment">// 方法2：先将char型数字转换为String字符串，再用Integer（int的包装类，提供了String转换为int的方法，后面教程中我们会详细讲解）转换为int型数字</span></li><li>            <span class="sh_comment">// int num = Integer.parseInt(String.valueOf(str[0].charAt(i)));</span></li><li>            <span class="sh_comment">// 方法3：char型数字减去字符0来转换为int型数字</span></li><li>            <span class="sh_type">int</span> num <span class="sh_symbol">=</span> str<span class="sh_symbol">[</span><span class="sh_number">0</span><span class="sh_symbol">].</span><span class="sh_function">charAt</span><span class="sh_symbol">(</span>i<span class="sh_symbol">)</span> <span class="sh_symbol">-</span> <span class="sh_string">'0'</span><span class="sh_symbol">;</span></li><li>            <span class="sh_comment">// 如果不是最后一位数字，而且数字不是零，则需要添加单位（千、百、十）</span></li><li>            <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>i <span class="sh_symbol">!=</span> zhengLen <span class="sh_symbol">-</span> <span class="sh_number">1</span> <span class="sh_symbol">&amp;&amp;</span> num <span class="sh_symbol">!=</span> <span class="sh_number">0</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>                result <span class="sh_symbol">+=</span> hanArr<span class="sh_symbol">[</span>num<span class="sh_symbol">]</span> <span class="sh_symbol">+</span> unitArr<span class="sh_symbol">[</span>zhengLen <span class="sh_symbol">-</span> <span class="sh_number">2</span> <span class="sh_symbol">-</span> i<span class="sh_symbol">];</span></li><li>            <span class="sh_cbracket">}</span> <span class="sh_keyword">else</span> <span class="sh_cbracket">{</span></li><li>                <span class="sh_comment">// 否则添加“元”</span></li><li>                result <span class="sh_symbol">+=</span> hanArr<span class="sh_symbol">[</span>num<span class="sh_symbol">]</span> <span class="sh_symbol">+</span> <span class="sh_string">"元"</span><span class="sh_symbol">;</span></li><li>            <span class="sh_cbracket">}</span></li><li>        <span class="sh_cbracket">}</span></li><li>        <span class="sh_comment">// 依次遍历小数的每一位数字</span></li><li>        <span class="sh_keyword">for</span> <span class="sh_symbol">(</span><span class="sh_type">int</span> i <span class="sh_symbol">=</span> <span class="sh_number">0</span><span class="sh_symbol">;</span> i <span class="sh_symbol">&lt;</span> xiaoLen<span class="sh_symbol">;</span> i<span class="sh_symbol">++)</span> <span class="sh_cbracket">{</span></li><li>            <span class="sh_type">int</span> num <span class="sh_symbol">=</span> str<span class="sh_symbol">[</span><span class="sh_number">1</span><span class="sh_symbol">].</span><span class="sh_function">charAt</span><span class="sh_symbol">(</span>i<span class="sh_symbol">)</span> <span class="sh_symbol">-</span> <span class="sh_string">'0'</span><span class="sh_symbol">;</span></li><li>            <span class="sh_comment">// 如果数字不是零，则需要添加单位（角，分）</span></li><li>            <span class="sh_keyword">if</span> <span class="sh_symbol">(</span>num <span class="sh_symbol">!=</span> <span class="sh_number">0</span><span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>                result <span class="sh_symbol">+=</span> hanArr<span class="sh_symbol">[</span>num<span class="sh_symbol">]</span> <span class="sh_symbol">+</span> unitArr2<span class="sh_symbol">[</span>xiaoLen <span class="sh_symbol">-</span> <span class="sh_number">1</span> <span class="sh_symbol">-</span> i<span class="sh_symbol">];</span></li><li>            <span class="sh_cbracket">}</span></li><li>        <span class="sh_cbracket">}</span></li><li>        <span class="sh_keyword">return</span> result<span class="sh_symbol">;</span></li><li>    <span class="sh_cbracket">}</span></li><li><span style="display:none;">&nbsp;</span></li><li>    <span class="sh_keyword">public</span> <span class="sh_keyword">static</span> <span class="sh_type">void</span> <span class="sh_function">main</span><span class="sh_symbol">(</span>String<span class="sh_symbol">[]</span> args<span class="sh_symbol">)</span> <span class="sh_cbracket">{</span></li><li>        <span class="sh_usertype">Scanner</span><span class="sh_normal"> </span>sc <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">Scanner</span><span class="sh_symbol">(</span>System<span class="sh_symbol">.</span>in<span class="sh_symbol">);</span></li><li>        <span class="sh_usertype">NumRmb</span><span class="sh_normal"> </span>nr <span class="sh_symbol">=</span> <span class="sh_keyword">new</span> <span class="sh_function">NumRmb</span><span class="sh_symbol">();</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"请输入一个浮点数："</span><span class="sh_symbol">);</span></li><li>        <span class="sh_usertype">String</span><span class="sh_normal"> </span>num <span class="sh_symbol">=</span> sc<span class="sh_symbol">.</span><span class="sh_function">next</span><span class="sh_symbol">();</span></li><li>        String<span class="sh_symbol">[]</span> strs <span class="sh_symbol">=</span> nr<span class="sh_symbol">.</span><span class="sh_function">divide</span><span class="sh_symbol">(</span>Double<span class="sh_symbol">.</span><span class="sh_function">parseDouble</span><span class="sh_symbol">(</span>num<span class="sh_symbol">));</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"分解成整数部分和小数部分结果为："</span> <span class="sh_symbol">+</span> Arrays<span class="sh_symbol">.</span><span class="sh_function">toString</span><span class="sh_symbol">(</span>strs<span class="sh_symbol">));</span></li><li>        System<span class="sh_symbol">.</span>out<span class="sh_symbol">.</span><span class="sh_function">println</span><span class="sh_symbol">(</span><span class="sh_string">"人民币读法为："</span> <span class="sh_symbol">+</span> nr<span class="sh_symbol">.</span><span class="sh_function">toHanStr</span><span class="sh_symbol">(</span>strs<span class="sh_symbol">));</span></li><li>    <span class="sh_cbracket">}</span></li><li><span class="sh_cbracket">}</span></li></ol></pre><pre class="snippet-textonly sh_sourceCode" style="display:none;">import java.util.Arrays;
import java.util.Scanner;

public class NumRmb {
    private String[] hanArr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    private String[] unitArr = { "十", "百", "千" };
    private String[] unitArr2 = { "分", "角" };

    /**
     * 把一个浮点数分解成整数部分和小数部分字符串
     *
     * @param num 需要被分解的浮点数
     * @return 分解出来的整数部分和小数部分。第一个数组元素是整数部分，笫二个数组元素是小数部分
     */
    private String[] divide(double num) {

        // 将一个浮点数强制类型转换为long型，即得到它的整数部分
        long zheng = (long) num;
        // 浮点数减去整数部分，得到小数部分，小数部分乘以100后再取整得到2位小数
        long xiao = Math.round((num - zheng) * 100);
        // 下面用了2种方法把整数转换为字符串
        return new String[] { zheng + "", String.valueOf(xiao) };
    }

    /**
     * 将数字转成人民币读法
     *
     * @param str[] 被分解成整数部分和小数部分的字符串数组
     * @return 人民币读法的汉字字符串
     */
    private String toHanStr(String str[]) {

        int zhengLen = str[0].trim().split("").length; // 整数的长度
        int xiaoLen = str[1].split("").length; // 小数的长度
        String result = "";
        // 依次遍历整数的每一位数字
        for (int i = 0; i &lt; zhengLen; i++) {
            // 把char型数字转换成int型数字，3 种方法如下：
            // 方法1：因为它们的ASCII码值恰好相差48，因此把char型数字减去48得到int型数字，例如‘4’被转换成4
            // int num = str[0].charAt(i) - 48;
            // 方法2：先将char型数字转换为String字符串，再用Integer（int的包装类，提供了String转换为int的方法，后面教程中我们会详细讲解）转换为int型数字
            // int num = Integer.parseInt(String.valueOf(str[0].charAt(i)));
            // 方法3：char型数字减去字符0来转换为int型数字
            int num = str[0].charAt(i) - '0';
            // 如果不是最后一位数字，而且数字不是零，则需要添加单位（千、百、十）
            if (i != zhengLen - 1 &amp;&amp; num != 0) {
                result += hanArr[num] + unitArr[zhengLen - 2 - i];
            } else {
                // 否则添加“元”
                result += hanArr[num] + "元";
            }
        }
        // 依次遍历小数的每一位数字
        for (int i = 0; i &lt; xiaoLen; i++) {
            int num = str[1].charAt(i) - '0';
            // 如果数字不是零，则需要添加单位（角，分）
            if (num != 0) {
                result += hanArr[num] + unitArr2[xiaoLen - 1 - i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        NumRmb nr = new NumRmb();
        System.out.println("请输入一个浮点数：");
        String num = sc.next();
        String[] strs = nr.divide(Double.parseDouble(num));
        System.out.println("分解成整数部分和小数部分结果为：" + Arrays.toString(strs));
        System.out.println("人民币读法为：" + nr.toHanStr(strs));
    }
}
</pre></div></div>
从上面程序的运行结果来看，初步实现了所需功能，但这个程序并不是这么简单的，对零的处理比较复杂。例如，有两个零连在一起时该如何处理呢？如果最高位是零如何处理呢？最低位是零又如何处理呢？因此，这个程序还需要继续完善，希望读者能把这个程序写完。</div>
			<div id="arc-append">
	<p>关注微信公众号「站长严长生」，在手机上阅读所有教程，随时随地都能学习。本公众号由<a class="col-link" href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">C语言中文网站长</a>运营，每日更新，坚持原创，敢说真话，凡事有态度。</p>
	<p style="margin-top:12px; text-align:center;">
		<img width="180" src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/qrcode_weixueyuan_original.png" alt="魏雪原二维码"><br>
		<span class="col-green">微信扫描二维码关注公众号</span>
	</p>
</div>
<!-- <p>关注微信公众号「魏雪原」，跟着<a class="col-link" href="/view/8092.html" target="_blank">站长</a>一起学习，拒绝躺平，跳出内卷，用知识完成阶级跃升：</p>
	<ul>
		<li>每日更新，坚持原创，敢说真话，凡事有态度。</li>
		<li>编程+求职+创业+营销，既有硬知识，也有软技能。</li>
		<li>手机端阅读教程，随时随地都能学习。</li>
		<li>即将绑定网站部分功能，包括注册登录、找回密码、获取下载链接等。</li>
	</ul> -->
			<div class="pre-next-page clearfix">
                    <span class="pre left"><span class="icon">&lt;</span> <span class="text-brief text-brief-pre">上一页</span><a href="http://c.biancheng.net/view/5855.html">Java求数组元素的最大和最小值</a></span>
                    <span class="next right"><a href="http://c.biancheng.net/view/915.html">Java统计商品信息</a><span class="text-brief text-brief-next">下一页</span> <span class="icon">&gt;</span></span>
                </div>
			<div id="ad-arc-bottom"></div>

<!-- <div id="ad-bottom-weixin" class="clearfix">
	<div class="left" style="width: 535px;">
		<p><span class="col-red">编程帮</span>，一个分享编程知识的公众号。跟着<a class="col-link" href="/cpp/about/author/" target="_blank">站长</a>一起学习，每天都有进步。</p>
		<p>通俗易懂，深入浅出，一篇文章只讲一个知识点。</p>
		<p>文章不深奥，不需要钻研，在公交、在地铁、在厕所都可以阅读，随时随地涨姿势。</p>
		<p>文章不涉及代码，不烧脑细胞，人人都可以学习。</p>
		<p>当你决定关注「编程帮」，你已然超越了90%的程序员！</p>
	</div>
	<div class="right" style="width: 150px;">
		<img width="150" src="/templets/new/images/erweima_biancheng.gif?v=1.6.72" alt="编程帮二维码" /><br />
		<span class="col-green">微信扫描二维码关注</span>
	</div>
</div> -->

<div id="nice-arcs" class="box-bottom">
	<h4>优秀文章</h4>
	<ul class="clearfix">
<li><a href="http://c.biancheng.net/view/41.html" title="Go语言fmt.Sprintf（格式化输出）">Go语言fmt.Sprintf（格式化输出）</a></li>
<li><a href="http://c.biancheng.net/view/450.html" title="什么是编译器？">什么是编译器？</a></li>
<li><a href="http://c.biancheng.net/view/660.html" title="GCC编译C语言程序完整演示">GCC编译C语言程序完整演示</a></li>
<li><a href="http://c.biancheng.net/view/1337.html" title="CSS样式优先级顺序">CSS样式优先级顺序</a></li>
<li><a href="http://c.biancheng.net/view/vip_1722.html" title="C语言和C++到底有什么关系？">C语言和C++到底有什么关系？</a></li>
<li><a href="http://c.biancheng.net/view/1793.html" title="C语言数据输出大汇总以及轻量进阶">C语言数据输出大汇总以及轻量进阶</a></li>
<li><a href="http://c.biancheng.net/view/4442.html" title="go generate命令——在编译前自动化生成某类代码">go generate命令——在编译前自动化生成某类代码</a></li>
<li><a href="http://c.biancheng.net/view/5365.html" title="Spring Cloud Hystrix（熔断器）介绍及使用">Spring Cloud Hystrix（熔断器）介绍及使用</a></li>
<li><a href="http://c.biancheng.net/view/7143.html" title="MySQL配置环境变量">MySQL配置环境变量</a></li>
<li><a href="http://c.biancheng.net/view/8173.html" title="JS使用函数封装图片切换功能">JS使用函数封装图片切换功能</a></li>
</ul>
</div>
		</div>
		
	</div>
</div>
<script type="text/javascript">
// 当前文章ID
window.arcIdRaw = 'a_' + 7059;
window.arcId = "4ecb4f4aBA6xcoj1L5+fL4BNY+eIdR1lWZBHTfQE1GBeTRTwXdtNkTs4j30";
window.typeidChain = "98|79";
</script>
<div id="footer" class="clearfix">
	<div class="info left">
	<p>精美而实用的网站，分享优质编程教程，帮助有志青年。千锤百炼，只为大作；精益求精，处处斟酌；这种教程，看一眼就倾心。</p>
	<p>
		<a href="http://c.biancheng.net/view/8066.html" target="_blank" rel="nofollow">关于网站</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8092.html" target="_blank" rel="nofollow">关于站长</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8097.html" target="_blank" rel="nofollow">如何完成一部教程</a> <span>|</span>
		<a href="http://c.biancheng.net/view/8093.html" target="_blank" rel="nofollow">联系我们</a> <span>|</span>
		<a href="http://c.biancheng.net/sitemap/" target="_blank" rel="nofollow">网站地图</a>
	</p>
	<p>Copyright ©2012-2021 biancheng.net, <a href="http://www.beian.miit.gov.cn/" target="_blank" rel="nofollow" style="color:#666;">陕ICP备15000209号</a></p>
	</div>
	<img class="right" src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/logo_bottom.gif" alt="底部Logo">
	<span id="return-top"><b>↑</b></span>
</div>

<script type="text/javascript">
window.siteId = 4;
window.cmsTemplets = "/templets/new";
window.cmsTempletsVer = "1.6.72";
</script>

<script src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/jquery1.12.4.min.js.下载"></script>
<script src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/common.js.下载"></script>
<span style="display:none;"><script src="./7.21Java项目实战：数字转人民币读法（附带源码和解析）_files/z_stat.php" type="text/javascript" defer="defer" async="async"></script></span>

</body></html>